package cn.cdzdjd.service.impl;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import cn.cdzdjd.constant.BrowserConstant;
import cn.cdzdjd.exception.ErrorCode;
import cn.cdzdjd.exception.RenException;
import cn.cdzdjd.service.DianPingService;
import cn.cdzdjd.util.JsoupUtil;
import cn.cdzdjd.util.UrlUtil;
import cn.cdzdjd.vo.VideoInfoVO;

@Service
public class DianPingServiceImpl implements DianPingService {
	private final String baseUrl = "https://m.dianping.com/smallvideo/api/poison/video";

	@Override
	public VideoInfoVO getVideoInfo(String url) {
		try {
			if(url.contains("smallvideo")) {
				Map<String, String> params = UrlUtil.convertUrlParamsToMap(new URL(url));
				JSONObject bodyJson = new JSONObject();
				bodyJson.put("videoId", params.get("videoId"));
				bodyJson.put("mainId", params.get("mainId"));
				bodyJson.put("feedType", "29");
				bodyJson.put("bizType", params.get("bizType"));
				Response response = Jsoup.connect(baseUrl).ignoreContentType(true).ignoreHttpErrors(true)
						.followRedirects(false).userAgent(BrowserConstant.USER_AGENT_MOBILE)
						.header("Accept", BrowserConstant.ACCEPT).referrer(baseUrl)
						.header("Content-Type", "application/json").method(Method.POST).requestBody(bodyJson.toString())
						.execute();
				String jsonStr = response.body();
				JSONObject json = new JSONObject(jsonStr);
				JSONObject data = json.getJSONObject("data");
				String videoTitle = data.getString("title");
				String videoUrl = data.getString("url");
				String posterUrl = data.getString("cover");
				List<String> videoList = new ArrayList<>();
				videoList.add(videoUrl);
				List<String> posterList = new ArrayList<>();
				posterList.add(posterUrl);
				return new VideoInfoVO(videoList, posterList, videoTitle, null);
			}else {
				Map<String, String> cookie = new HashMap<>();
				cookie.put("webp_support", "0|2");
				Document document = JsoupUtil.getDocumentCookie(url,cookie);
				Element body = document.body();
				Elements scripts = body.getElementsByTag("script");
				String data = scripts.get(3).data();
				int start = StringUtils.ordinalIndexOf(data, "{", 1);
				int end = StringUtils.lastOrdinalIndexOf(data, "}", 1);
				String jsonStr = StringUtils.substring(data, start, end+1);
				JSONObject json = new JSONObject(jsonStr);
				JSONObject dump = json.getJSONObject("dump");
				JSONObject nine = dump.getJSONObject("9");
				String videoTitle = nine.getString("title");
				JSONArray feedPicList = nine.getJSONArray("feedPicList");
				List<String> posterList = new ArrayList<>();
				for (int i = 0; i < feedPicList.length(); i++) {
					JSONObject feedPic = feedPicList.getJSONObject(i);
					posterList.add(feedPic.getString("url").replace("\u002F", "/"));
				}
				return new VideoInfoVO(null, posterList, videoTitle, null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RenException(ErrorCode.PARSE_ERROR);
		}
	}
}
